﻿using HotPot.HttpClient.Net45;
using HotPot.HttpClient.Net45.Contract;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dandan
{
    public partial class New_AddPreinstallCommodity : Form
    {
        public MainForm1 MainForm;
        public InboundModelsData m_ModelsData = new InboundModelsData();

        public New_AddPreinstallCommodity()
        {
            InitializeComponent();
        }

        private void New_AddPreinstallCommodity_Load(object sender, EventArgs e)
        {
            if (m_ModelsData != null)
            {
                textBox8.Text = m_ModelsData.group.main_sn;
                textBox10.Text = m_ModelsData.group.sub_sn;
                textBox1.Text = m_ModelsData.baseinfo.brand.title;
                textBox2.Text = m_ModelsData.baseinfo.title;
                textBox3.Text = m_ModelsData.baseinfo.sn;
                textBox4.Text = m_ModelsData.group.fields[0].value;
                textBox5.Text = m_ModelsData.group.fields[1].value;
                if (m_ModelsData.group.shopinfo != null && m_ModelsData.group.shopinfo.Count > 0)
                {
                    textBox6.Text = m_ModelsData.group.shopinfo[0].base_price;
                    textBox7.Text = m_ModelsData.group.shopinfo[0].sale_price;
                    comboBox1.Text = m_ModelsData.group.shopinfo[0].discount_value;
                    textBox9.Text = m_ModelsData.group.shopinfo[0].stock;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_ModelsData.group.main_sn = textBox8.Text;
            m_ModelsData.group.sub_sn = textBox10.Text;
            m_ModelsData.baseinfo.brand.title = textBox1.Text;
            m_ModelsData.baseinfo.title = textBox2.Text;
            m_ModelsData.baseinfo.sn = textBox3.Text;
            m_ModelsData.shop_id = UserData.GetData().data.shops[0].id;

            m_ModelsData.group.fields[0].value = textBox4.Text;
            m_ModelsData.group.fields[1].value = textBox5.Text;
            m_ModelsData.group.shop.base_price = textBox6.Text;
            m_ModelsData.group.shop.sale_price = textBox7.Text;
            m_ModelsData.group.shop.discount_value = comboBox1.Text;
            m_ModelsData.group.shop.stock = textBox9.Text;
            m_ModelsData.group.shop.shop_id = UserData.GetData().data.shops[0].id;


            string strJson = JsonConvert.SerializeObject(m_ModelsData);
            GetCommoditysData(strJson);
            //MainForm.Test();
        }

        public void GetCommoditysData(string strJson)
        {
            String Url = "/api/v1/projects";

            IHttpClient httpClient = new HotPot.HttpClient.Net45.HttpClient();

            Dictionary<string, string> headers = new Dictionary<string, string>();
            if (LoginData.GetLoginData().data != null)
            {
                headers.Add("Authorization", "Bearer " + LoginData.GetLoginData().data.token);

            }
            var postString = httpClient.PostString("https://tinyboss.cn" + Url, strJson, headers: headers);

            try
            {
                InboundModelsReturnData m_InboundModelsReturnData = JsonConvert.DeserializeObject<InboundModelsReturnData>(postString);
                if (m_InboundModelsReturnData.code.Equals("200"))
                {
                    MainForm.GetCommoditysData();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("系统提示" + ex);
            }
            //MessageBox.Show(postString);

        }

    }
}
